using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shop : MonoBehaviour
{
    public GameObject itemPrefab;
    public GameObject imformation;
    public int cost;
    public LayerMask playerLayer;
    private GameObject item;
    public Collider2D coll;
    private Collider2D playerColl;
    // Start is called before the first frame update
    void Start()
    {
        imformation.SetActive(false);
        item = Instantiate(itemPrefab, transform.position + new Vector3(0, 0.5f, 0), transform.rotation);
        item.GetComponent<Collider2D>().enabled = false;
    }

    // Update is called once per frame
    void Update()
    {
        if (coll.IsTouchingLayers(playerLayer))
        {
            imformation.SetActive(true);
            if (Input.GetKeyDown(KeyCode.E) && playerColl.GetComponent<PlayerCon>().parameterPlayer.coin >= cost)
            {
                playerColl.GetComponent<PlayerCon>().parameterPlayer.coin -= cost;
                item.GetComponent<Collider2D>().enabled = true;
                item.transform.position -= new Vector3(0, 1, 0);
                coll.enabled = false;
            }
        }
        else
        {
            imformation.SetActive(false);
        }
    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Player"))
        {
            playerColl = collision;
        }
    }
}
